using Microsoft.Extensions.DependencyInjection;
using Serilog;
using Serilog.Events;
using System.Diagnostics;
using System.Reflection;
using Volo.Abp;

namespace AbpCodeGenerater
{
    internal static class Program
    {
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // To customize application configuration such as set high DPI settings or default font,
            // see https://aka.ms/applicationconfiguration.
            ApplicationConfiguration.Initialize();

            Log.Logger = new LoggerConfiguration()
#if DEBUG
            .MinimumLevel.Debug()
#else
            .MinimumLevel.Information()
#endif
            .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
            .Enrich.FromLogContext()
            .WriteTo.Async(c => c.File("Logs/logs.txt", rollingInterval: RollingInterval.Day))
            .CreateLogger();

            Log.Information("Starting AbpCodeGenerater.");

            var abpApplication = AbpApplicationFactory.Create<AbpCodeGeneraterModule>(options =>
            {
                options.UseAutofac();
                options.Services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(dispose: true));
            });

            abpApplication.Initialize();

            Application.ApplicationExit += new EventHandler(async (sender, e) =>
            {
                await abpApplication.ShutdownAsync();
                Log.CloseAndFlush();
            });

            var startUpWindow = abpApplication.Services.GetRequiredService<MainWindow>();

            if (startUpWindow != null)
            {
                Application.Run(startUpWindow);
            }
            else
            {
                Log.Information("Unable to find ControlWindow as startup window.");
            }
        }
    }
}